package org.ly.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    public static long string2timestamp(String dateTimeString){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timestamp = -1;
        try {
            // 解析字符串为日期对象
            Date date = dateFormat.parse(dateTimeString);
            // 获取时间戳（毫秒数）
            timestamp = date.getTime();
//            System.out.println("时间戳: " + timestamp);
        } catch (ParseException e) {
            LogUtil.logError(e.toString(), e);
        }
        return timestamp;
    }

    public static String timestamp2string(long timestamp){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        String dateTimeString = "";
        try {
            date = new Date(timestamp);
            dateTimeString = dateFormat.format(date);
        } catch (Exception e) {
            LogUtil.logError(e.toString(), e);
        }
        return dateTimeString;
    }
}
